package com.gun.management.module.raffle.dal.mysql.lotteryrule;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gun.management.framework.common.pojo.PageResult;
import com.gun.management.framework.mybatis.core.mapper.BaseMapperX;
import com.gun.management.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.gun.management.module.raffle.controller.admin.lotterylog.vo.LotteryLogRespVO;
import com.gun.management.module.raffle.controller.admin.lotteryrule.vo.GoodsPageReqVO;
import com.gun.management.module.raffle.controller.admin.lotteryrule.vo.GoodsRespVO;
import com.gun.management.module.raffle.controller.admin.lotteryrule.vo.LotteryRulePageReqVO;
import com.gun.management.module.raffle.controller.app.vo.AppLotteryPrizeRespVO;
import com.gun.management.module.raffle.controller.app.vo.LotteryGoodsRespVO;
import com.gun.management.module.raffle.dal.dataobject.lottery.LotteryDO;
import com.gun.management.module.raffle.dal.dataobject.lotteryrule.LotteryRuleDO;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 抽奖规则 Mapper
 *
 * @author 测试账号
 */
@Mapper
public interface LotteryRuleMapper extends BaseMapperX<LotteryRuleDO> {

    @Insert("INSERT INTO raffle_lottery_rule(`avatar`, `name`, product_id,`sku_id`, `category_id`,  `prompt`, `prize_probability`, `prize_qty`, `status` ) VALUES " +
            "(#{entity.avatar}, #{entity.name},#{entity.productId}, #{entity.skuId}, #{entity.categoryId},  #{entity.prompt}, #{entity.prizeProbability}, #{entity.prizeQty}, #{entity.status} );")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    public void insertAndGetId(@Param("entity") LotteryRuleDO entity);

    @Select({"<script>select A.sku_id,B.product_id from raffle_lottery_rule A " +
            " LEFT JOIN eb_goods_attr_value B on A.sku_id = B.id " +
            " and A.product_id = B.product_id  " +
            " and A.tenant_id = B.tenant_id where A.id = #{ruleId} </script>"})
    public LotteryGoodsRespVO selectLotteryGoodsRespVO(@Param("ruleId") Long  ruleId);

    @Select({"<script> SELECT " +
            "  B.id, " +
            "  B.product_id, " +
            "  B.sku_id, " +
            "  B.avatar, " +
            "  B.`name`," +
            "  C.sku, " +
            "  B.prompt, " +
            "  B.category_id, " +
            "  B.prize_qty, " +
            "  B.prize_probability  " +
            "FROM " +
            "  raffle_lottery_rule_ref A " +
            "  inner JOIN raffle_lottery_rule B ON A.rule_id = B.id  " +
            "  AND A.tenant_id = B.tenant_id " +
            "  LEFT JOIN eb_goods_attr_value C ON B.product_id = C.product_id  " +
            "  AND B.sku_id = C.id  " +
            "  AND A.tenant_id = C.tenant_id  " +
            " where A.lottery_id = #{lotteryId} " +
            " order by B.create_time asc " +
            "</script>"}
    )
    public List<LotteryRuleDO> selectLotteryRuleList(@Param("lotteryId") Long  lotteryId );

    /**
     * 根据产品id,名称，商品分类 分获取产品及其sku列表
     * @param reqVO
     * @return
     */
    @Select({"<script> SELECT " +
            "    concat(A.id, '-', B.id) pkId," +
            "    A.id, " +
            "    A.`name`, " +
            "    A.image, " +
            "    A.cate_id, " +
            "    A.category_id," +
            "    A.price," +
            "    A.stock, " +
            "    B.id sku_id, " +
            "    B.sku " +
            "        FROM " +
            "    eb_goods A " +
            "    LEFT JOIN eb_goods_attr_value B ON A.id = B.product_id " +
            "    AND A.tenant_id = B.tenant_id " +
            "  <where>" +
            " A.is_show = 1  and  A.deleted = 0  " +
            "<if test='reqVO.id != null '>" +
            " and A.id=  #{reqVO.id}  " +
            "</if>" +
            "<if test='reqVO.name != null and reqVO.name != \"\"'>" +
            " and A.name like concat('%',#{reqVO.name},'%')" +
            "</if>" +
            "<if test='reqVO.categoryId != null '>" +
            " and A.category_id =  #{reqVO.categoryId}  " +
            "</if>" +
            "<if test='reqVO.isVirtual != null '>" +
            " and A.is_virtual =  #{reqVO.isVirtual}  " +
            "</if>" +
            "  </where>" +
            "    order by A.id,B.id limit  #{fromRec},  #{toRec} " +
            "</script>"}
    )
    public List<GoodsRespVO> selectGoodsPageList(@Param("reqVO") GoodsPageReqVO reqVO , @Param("fromRec") Integer fromRec, @Param("toRec") Integer toRec  );


    @Select({"<script> SELECT count(1) " +
            "        FROM " +
            "    eb_goods A " +
            "    LEFT JOIN eb_goods_attr_value B ON A.id = B.product_id " +
            "    AND A.tenant_id = B.tenant_id " +
            "  <where>" +
            " A.is_show = 1  and  A.deleted = 0  " +
            "<if test='reqVO.id != null '>" +
            " and A.id=  #{reqVO.id}  " +
            "</if>" +
            "<if test='reqVO.name != null and reqVO.name != \"\"'>" +
            " and A.name like concat('%',#{reqVO.name},'%')" +
            "</if>" +
            "<if test='reqVO.categoryId != null '>" +
            " and A.category_id =  #{reqVO.categoryId}  " +
            "</if>" +
            "<if test='reqVO.isVirtual != null '>" +
            " and A.is_virtual =  #{reqVO.isVirtual}  " +
            "</if>" +
            "  </where>" +
            "    order by A.id,B.id " +
            "</script>"}
    )
    public Long getTotal( @Param("reqVO") GoodsPageReqVO reqVO );

    // 此处要改
    default PageResult<LotteryRuleDO> selectPage(LotteryRulePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<LotteryRuleDO>()
                .eqIfPresent(LotteryRuleDO::getAvatar, reqVO.getAvatar())
                .likeIfPresent(LotteryRuleDO::getName, reqVO.getName())
                .eqIfPresent(LotteryRuleDO::getCategoryId, reqVO.getCategoryId())
                .eqIfPresent(LotteryRuleDO::getPrompt, reqVO.getPrompt())
                .eqIfPresent(LotteryRuleDO::getPrizeProbability, reqVO.getPrizeProbability())
                .eqIfPresent(LotteryRuleDO::getStatus, reqVO.getStatus())
                .eqIfPresent(LotteryRuleDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(LotteryRuleDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(LotteryRuleDO::getId));
    }

    @Select(" SELECT " +
            " C.id prize_id, " +
            " C.avatar," +
            " C.name, " +
            " C.product_id, " +
            " C.sku_id," +
            " V.sku, " +
            " C.prize_probability, " +
            " B.id lottery_id, " +
            " B.expense_score, " +
            " B.total_times, " +
            " B.day_total_times, " +
            " C.prize_qty, " +
            " C.category_id prize_category," +
            " C.prompt " +
            " FROM " +
            " raffle_lottery_rule_ref A " +
            " INNER JOIN raffle_lottery B ON A.lottery_id = B.id " +
            " AND A.tenant_id = B.tenant_id " +
            " INNER JOIN raffle_lottery_rule C ON A.rule_id = C.id " +
            " AND A.tenant_id = C.tenant_id " +
            " LEFT JOIN eb_goods_attr_value V ON C.product_id = V.product_id and " +
            " C.sku_id = V.id  AND C.tenant_id = V.tenant_id " +
            " where A.lottery_id = #{lotteryId} and B.status=0  and   A.tenant_id = #{tenantId} ")
    public List<AppLotteryPrizeRespVO> selectAppLotteryPrizeList(@Param("lotteryId") long lotteryId, @Param("tenantId") long tenantId);

}